//
//  SetPreferences.m
//  Bibliotecha
//
//  Created by Jaime Garnica on 4/27/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "SetPreferences.h"


@implementation SetPreferences

#pragma mark Received Actions

-(IBAction) changeLibraryPath:(id)sender
{
	int result;
    NSOpenPanel *oPanel = [NSOpenPanel openPanel];
	
    [oPanel setAllowsMultipleSelection:NO];
	[oPanel setCanChooseFiles:NO];
	[oPanel setCanChooseDirectories:YES];
	
    result = [oPanel runModalForDirectory:NSHomeDirectory()
									 file:nil types:nil];
    if (result == NSOKButton) 
	{
        NSArray *path = [oPanel filenames];
        
		[[NSUserDefaults standardUserDefaults] setObject:[path objectAtIndex:0] forKey:@"libraryPath"];
    }
	[currentPath setTitle:[[NSUserDefaults standardUserDefaults] stringForKey:@"libraryPath"]];
	[pathButton selectItem:currentPath];
}


/*-(IBAction) organizeLibrary:(id)sender
{
	if( [keepLibOrganized state] == NSOnState )
	{
		[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"keepLibOrganized"];
		[deleteBooksOnAdd setEnabled:YES];
	}
	
	else
	{ 
		[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"keepLibOrganized"];
		[deleteBooksOnAdd setEnabled:YES];
	}
}*/


-(IBAction) deleteBooks:(id)sender
{
	if( [deleteBooksOnAdd state] == NSOnState )
	{
		[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"deleteOnAdd"];
	}
	
	else
	{ 
		[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"deleteOnAdd"];
	}
}


#pragma mark Methods

-(void) initPrefs
{
	[NSBundle loadNibNamed:@"Preferences" owner:self];	//get me a preferences window!
	
	[prefsWindow setExcludedFromWindowsMenu:YES];
	[prefsWindow makeKeyAndOrderFront:nil];
	
	[deleteBooksOnAdd setEnabled:YES];
		
	if( [[NSUserDefaults standardUserDefaults] boolForKey:@"deleteOnAdd"] )
	{
		[deleteBooksOnAdd setState:NSOnState];
	}
		
	else [deleteBooksOnAdd setState:NSOffState];
	
	[currentPath setTitle:[[NSUserDefaults standardUserDefaults] stringForKey:@"libraryPath"]];
}

@end
